/*
 * @(#)Crypt.java
 *
 * Copyright (c) 2003 DCIVision Ltd
 * All rights reserved.
 *
 * This software is the confidential and proprietary information of DCIVision
 * Ltd ("Confidential Information").  You shall not disclose such Confidential
 * Information and shall use it only in accordance with the terms of the license
 * agreement you entered into with DCIVision Ltd.
 */
package com.dcivision.framework;

/**
  Crypt.java

  This class is to provide encrypt and decrypt function by a password provided.

    @author          Rollo Chan
    @company         DCIVision Limited
    @creation date   25/06/2003
    @version         $Revision: 1.6 $
*/

public class Crypt {

  public static final String REVISION = "$Revision: 1.6 $";

  private Crypt() {
  }

  /**
   * encrypt
   *
   * Encrypt a string with a password
   *
   * @param buffer    The buffer to be encrypted
   * @param password  The password
   */
  public static String encrypt(String buffer, String password) {
    StringBuffer sbSpace = new StringBuffer("     ");
    buffer = sbSpace.toString() + buffer + sbSpace.toString();
    char[] temp = new char[buffer.length()];

    int a = 0;
    for (int i = 0; i<buffer.length(); i++) {
      int b = password.charAt(a);
      a ++;
      if (a >= password.length()) {
        a = 0;
      }
      int c = (int)buffer.charAt(i);
      temp[i] = (char)(c ^ b);
    }
    return toHex(temp);
  }

  /**
   * decrypt
   *
   * Decrypt a string with a password
   *
   * @param buffer    The buffer to be decrypted
   * @param password  The password
   */
  public static String decrypt(String buffer, String password) {
    char [] temp = toChar(buffer);
    String result = "";
    int a = 0;
    for (int i = 0; i<temp.length; i ++) {
      int b = password.charAt(a);
      a ++;
      if (a>=password.length()) {
        a = 0;
      }
      int c = temp[i];
      result += (char)(c ^ b);
    }
    return result.trim();
  }

  private static int hexToInt(char c) {
    if (c>='0' && c<='9') {
      return c - '0';
    }
    return (c - 'a') + 10;
  }

  private static char hexToChar(String buffer) {
    int l = hexToInt(buffer.charAt(1));
    int h = hexToInt(buffer.charAt(0));
    return (char)(h*16 + l);
  }

  private static String toHex(char[] buffer) {
    String temp = "";
    String result = "";
    for (int i = 0; i<buffer.length; i++) {
      int c = buffer[i];
      temp = Integer.toHexString((int)c);
      if (temp.length() == 1) {
        temp = "0" + temp;
      }
      result += temp;
    }
    return result;
  }

  private static char [] toChar(String buffer) {
    char [] result = new char[buffer.length()/2];
    for (int i = 0; i<buffer.length(); i += 2) {    
      if(i+2<=buffer.length()){
        String temp = buffer.substring(i, i+2);
        result[i/2] = (char)hexToChar(temp);
      }      
    }
    return result;
  }

  /**
   * main
   *
   * @param arg
   */
  public static void main(String arg[]) {
    String buffer = arg[0];
    String password = arg[1];
    String temp = Crypt.encrypt(buffer, password);
    System.out.println("buffer    = \"" + buffer + "\"");
    System.out.println("password  = \"" + password + "\"");
    System.out.println("encrypted = \"" + temp + "\"");
    System.out.println("decrypted = \"" + Crypt.decrypt(temp, password) + "\"");
  }
}